import copy
import engine
import os

def loadWorld(dir):
	world = engine.World()
	
	files = os.listdir(dir)
	#print files
	for fn in files:
		try:
			f = open( os.path.join(dir, fn) )
			name = os.path.splitext(fn)[0]
			worldFile = engine.WorldFile(name)
			world.worldFiles[name] = worldFile
			loadWorldFile(worldFile, f)
		except IOError:
			pass
	
	world.worldFiles['core'] = engine.WorldFile('core')
	
	limbo = engine.Zone(world, world.worldFiles['core'], 'limbo')
	limbo.lookmsg.append(engine.LookMessage('You are floating in a void.'))
	limbo.fightprob = 1
	world.worldFiles['core'].zones['limbo'] = limbo
	world.defaultZone = limbo
	
	hitability = engine.Ability()
	#target single other
	hitability.usable.add('fight')
	hitability.attackattr, hitability.defendattr, hitability.attackpower, hitability.attacktype = 'str', 'pdef', 1, 'hit'
	hitability.sortorder = 0
	world.worldFiles['core'].abilities['hit'] = hitability
	
	guardability = engine.Ability()
	#target self
	guardability.usable.add('fight')
	guardability.usestatus = engine.Status()
	guardability.usestatus.name = 'guard'
	guardability.usestatus.duration = 'turn'
	guardability.usestatus.attrmult['pdef'] = 2
	guardability.sortorder = 2
	guardability.usestring = '%(doer)s guards.'
	world.worldFiles['core'].abilities['guard'] = guardability
	
	heal = engine.Ability()
	#target other, 'all' hits friendlies
	heal.usable.add('fight')
	heal.usable.add('zone')
	heal.attackattr, heal.defendattr, heal.attackpower, heal.attacktype = 'mag', 'mdef', 2.5, 'heal'
	heal.hitverb = 'heals'
	heal.usestring = '%(doer)s casts heal-- %(mpcost)s' 
	heal.mpcost = 2
	heal.sortorder = 0
	world.worldFiles['core'].abilities['heal'] = heal
	
	fire = engine.Ability()
	#target other, 'all' hits hostiles
	fire.usable.add('fight')
	fire.attackattr, fire.defendattr, fire.attackpower, fire.attacktype = 'mag', 'mdef', 1, 'fire'
	fire.hitverb = 'burn'
	fire.hitstring = "%(targ)s is %(verb)sed for %(dam)s!"
	fire.usestring = '%(doer)s casts fire-- %(mpcost)s' 
	fire.mpcost = 2
	fire.sortorder = 0
	world.worldFiles['core'].abilities['fire'] = fire
	
	alive = engine.Status()
	alive.name = 'alive'
	alive.statusflags['resist'] = {'heal': -1}
	
	beast = engine.Monster()
	beast.magdef = 5
	beast.hitverb = 'claws'
	for abil in ['hit']:
		beast.abilities[abil] = world.worldFiles['core'].abilities[abil]
	beast.status['alive'] = copy.copy(alive)
	world.worldFiles['core'].creatures['beast'] = beast
	
	tester = engine.Role()
	tester.hp, tester.hpmax = 600,600
	tester.mp, tester.mpmax = 10,10
	tester.str = 14
	for abil in ['hit', 'guard']:
		tester.abilities[abil] = world.worldFiles['core'].abilities[abil]
	tester.abilities['white'] = {'heal': world.worldFiles['core'].abilities['heal']}
	tester.abilities['black'] = {'fire': world.worldFiles['core'].abilities['fire']}
	tester.status['alive'] = copy.copy(alive)
	world.worldFiles['core'].creatures['role_tester'] = tester
	world.roles['tester'] = tester
	
	return world

def loadWorldFile(worldfile, fileobj):
	pass